package jsu.yym.tiktok.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiniu.common.QiniuException;
import jsu.yym.tiktok.mapper.UserDataMapper;
import jsu.yym.tiktok.mapper.VideoMapper;
import jsu.yym.tiktok.pojo.ResponseResult;
import jsu.yym.tiktok.pojo.User;
import jsu.yym.tiktok.mapper.Type;
import jsu.yym.tiktok.mapper.UserMapper;
import jsu.yym.tiktok.pojo.UserData;
import jsu.yym.tiktok.pojo.Video;
import jsu.yym.tiktok.service.UserService;
import jsu.yym.tiktok.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
public class userController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserDataMapper userDataMapper;
    @Autowired
    private VideoMapper videoMapper;

    //获取所有用户
    @GetMapping("/user/get/all")
    public ResponseResult getUserAll()
    {
        List<User> users = userMapper.selectList(null);
        if(Objects.isNull(users))
        {
            return new ResponseResult(503,"获取失败");
        }
        return new ResponseResult(200,"获取成功",users);
    }

    //根据id获取用户
    @GetMapping("/user/information")
    public ResponseResult getUserByID()
    {
        String id = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
            id = authentication.getName(); // 获取当前用户的id
        User user = userMapper.getUserByUserName(id);
        if(Objects.isNull(user))
            return new ResponseResult(503,"获取失败");
        return new ResponseResult(200,"获取成功",user);
    }

    //修改user
    @PostMapping("/user/update")
    public ResponseResult updateUser(@RequestBody User user)
    {
        int count = userMapper.updateById(user);
        userService.updateUserInElasticsearch(user);
        if(count<=0)
            return new ResponseResult(503,"获取失败");
        return new ResponseResult(200,"获取成功");
    }

    //根据昵称获取UserList
    @GetMapping("/user/get/name/{name}")
    public ResponseResult getUserByName(@PathVariable String name)
    {
        List<User> users = userMapper.getUserByNickName(name);
        if(Objects.isNull(users))
            return new ResponseResult(503,"获取失败");
        return new ResponseResult(200,"获取成功",users);
    }

//    //用户注销
//    @PostMapping("/user/delete")
//    public ResponseResult deleteUserByUsername(@RequestBody User user)
//    {
//        int count = userMapper.updateById(user);
//        if(count<=0)
//            return new ResponseResult(503,"注销失败");
//        return new ResponseResult(200,"注销成功");
//    }

    //上传头像
    @RequestMapping(value = "/user/upload",method = RequestMethod.POST)
    public ResponseResult uploadFile(@RequestParam("file") MultipartFile file) throws QiniuException {
        // 获取当前用户的id
        String id = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
            id = authentication.getName();
        //调用方法
        User user = userService.uploadImage(file,id);
        //更新头像和头像文件名
        int count = userMapper.updateImgAndFileNameByUsername(user.getImage(), user.getFileName(), id);
        if (count<=0) {
            return new ResponseResult(503,"上传失败");
        }
        return new ResponseResult(200,"上传成功",user.getImage());
    }

    //获取前端发来的数据，并存到数据库中
    @PostMapping("user/date/get/{ID}")
    public ResponseResult getUserData(@PathVariable int ID)
    {
        String id = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
            id = authentication.getName(); // 获取当前用户的id
        System.out.println("id"+id);
        //判重
        UserData userData = userDataMapper.getUserDataByNameAndID(id,ID);
        System.out.println(userData);
        if (!Objects.isNull(userData)) {
            return new ResponseResult(409,"重复添加");
        }
        UserData u = new UserData();
        u.setUsername(id);
        u.setVideo_id(ID);
        userDataMapper.insertUserData(u);
        return new ResponseResult(200,"成功添加");
    }


    //获取用户浏览历史
    @GetMapping("user/date/history/get")
    public ResponseResult getUserDataHistory()
    {
        String id = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
            id = authentication.getName(); // 获取当前用户的id
        System.out.println("id"+id);
        List<Integer>videoIDs = userDataMapper.getUserDataByIds(id);
        List<Video> videos = videoMapper.getVideosByPkIds(videoIDs);
        if(videos.isEmpty())
            return new ResponseResult(409,"还没有浏览记录");
        return new ResponseResult(200,"成功获取",videos);
    }

}
